<template>
  <div class="login-container">
    <video src="@/assets/img/video.3cf57b17.mp4" autoplay="autoplay" loop="loop" muted="muted" style="width: 100%; height: 100%; object-fit: fill;" />
    <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="">
        </h3>
      </div>

      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input ref="account" v-model="loginForm.account" placeholder="账号" name="account" type="text" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <span class="svg-container">
          <!-- <svg-icon icon-class="" /> -->
          <i class="el-icon-view" />
        </span>
        <el-input
          ref="code"
          v-model="loginForm.code"
          placeholder="验证码"
          name="code"
        />
        <span class="show-img" @click="changeCode">
          <img id="canvas_img" :src="str" alt="">
        </span>
      </el-form-item>

      <el-button class="but" :loading="loading" type="primary" style="width:400px;" @click.native="handleLogin">登录</el-button>

      <div class="tips">
        <span>仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</span>
      </div>
    </el-form>
    <div style="font-size: 14px; color: white; text-align: center; position: absolute; bottom: 20px; left: 0%; width: 100%; opacity: 0.9;">
      <span>江苏传智播客教育科技股份有限公司</span><span data-v-81300996="" style="margin-left: 20px;">版权所有Copyright 2006-2022 All Rights Reserved</span><span data-v-81300996="" style="margin-left: 20px;">苏ICP备16007882号-11</span>
    </div>
  </div>
</template>

<script>
import { code } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        key: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 5, max: 13, message: '账号长度位5-13位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度应该为6-16位之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      str: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 给key赋值
    this.loginForm.key = this.getRandomString(30)
    this.changeCode()
  },
  methods: {
    // 登录
    async login() {
      await this.$store.dispatch('user/setLogin', this.loginForm)
      this.$router.push('/')
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        valid && this.login()
      })
    },
    // 获取验证码
    async changeCode() {
      const res = await code(this.loginForm.key)
      // 赋值
      console.log(res)
      this.str = URL.createObjectURL(res.data)
      // console.log(this.str)
    },
    // 密码切换  显示隐藏切换
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    // 随机字符串
    getRandomString(len) {
      const _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789'
      const min = 0
      const max = _charStr.length - 1
      let _str = '' // 定义随机字符串 变量
      // 判断是否指定长度，否则默认长度为15
      len = len || 15
      // 循环生成字符串
      for (var i = 0, index; i < len; i++) {
        index = (function(randomIndexFunc, i) {
          return randomIndexFunc(min, max, i, randomIndexFunc)
        })(function(min, max, i, _self) {
          let indexTemp = Math.floor(Math.random() * (max - min + 1) + min)
          const numStart = _charStr.length - 10
          if (i === 0 && indexTemp >= numStart) {
            indexTemp = _self(min, max, i, _self)
          }
          return indexTemp
        }, i)
        _str += _charStr[index]
      }
      return _str
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#fff;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #000;
      height: 47px;
      // caret-color: $bg;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 400px;
    margin: 0 auto;
    border: 1px solid rgba(59, 56, 56, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #b6b3b3;
    margin-bottom: 20px;
  }
}
</style>

<style lang="scss" scoped>
// $bg:#f84e30;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;
  video{
    position: fixed;
    width: 100%;
    top: 0;
  }
  .but{
    margin-top: 30px;
    background-color: #eb3131;
    height: 50px;
    border: none;
  }

  .login-form {
    position: relative;
    border-radius: 3%;
    width: 450px;
    height: 490px;
    max-width: 100%;
    // padding: 160px 35px 0;
    text-align: center;
    margin: 260px auto;
    overflow: hidden;
    background-color: #fff;
  }

  .tips {
    margin: 10px 25px;
    font-size: 14px;
    color: #e60c0c;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    // color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 35px auto 40px auto;
      text-align: center;
      font-weight: bold;
          img{
      width: 180px;
      height: 54px;
    }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    // color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .show-img{
    display: inline-block;
    vertical-align: top;
    width: 71px;
    height: 32px;
    cursor: pointer;
    margin-left: 10px;
    position: absolute;
    right: 10px;
    top: 9px;
    img{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
